<?php
	include_once("Database.inc");
	
class CaseType {
	
	public $id;
	public $name;
	
	public static function getAllCaseTypes() {
		$p = array();
		
		$query = $GLOBALS["DB"] -> doQuery("SELECT CASETYPEID, NAME FROM CASETYPE");
		
		while ($row = mssql_fetch_array($query)) {
			array_push($p, CaseType::getCaseTypeFromArray($row));
		}
		
		return $p;
	}
	
	public static function getCaseType($id) {
		$query = $GLOBALS["DB"] -> doQuery("SELECT CASETYPEID, NAME FROM CASETYPE WHERE CASETYPEID=$id");
		
		if ($row = mssql_fetch_array($query)) {
			return CaseType::getCaseTypeFromArray($row);
		} else {
			return null;
		}
	}
	
	/*
	  * Deletes the case type with the specified ID.
	  */
	 public static function deleteCaseType($id) {
	 	$query = "DELETE FROM CASETYPE WHERE CASETYPEID=$id;";
	 	
	 	$GLOBALS["DB"] -> doQuery($query);
	 }
	 
	 /*
	 * Updates the specified case type in the database.  If the ID is empty, then
	 * the case type is added.  Otherwise, an existing record is modified.
	 */
	 public static function updateCaseType($caseType) {	 
	 	$query = "";
	 	if ($caseType->id == "") {
	 		$query = 
	 			"INSERT INTO CASETYPE(NAME) ".
				"VALUES ('$caseType->name');";
	 	} else {
	 		$query = 
	 			"UPDATE CASETYPE SET NAME='$caseType->name' WHERE casetypeid=$caseType->id;";
	 	}
	 	
	 	$GLOBALS["DB"] -> doQuery($query);
	 }
	
	protected static function getCaseTypeFromArray($array) {
		$p = new CaseType();
		
		$p->id = $array[0];
		$p->name = $array[1];
		
		return $p;
	}
}